package com.tiantianyuegou.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.widget.Adapter;
import android.widget.ListView;
import android.widget.SectionIndexer;

public class IndexScroller {
	private float mIndexbarWidth;
	private float mIndexbarMargin;
	private float mPreviewPadding;
	private float mDensity;
	private float mScaledDensity;
	private float mAlphaRate = 1;
	private int mListViewWidth;
	private int mListViewHeight;
	private int mCurrentSection = -1;
	private boolean mIsIndexing = false;
	private ListView mListView = null;
	private SectionIndexer mIndexer = null;
	private String[] mSections = null;
	private RectF mIndexbarRect;

	public IndexScroller(Context context, ListView lv) {
		mDensity = context.getResources().getDisplayMetrics().density;
		mScaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
		mListView = lv;
		setAdapter(mListView.getAdapter());

		mIndexbarWidth = 20 * mDensity;
		mIndexbarMargin = 5 * mDensity;
		mPreviewPadding = 5 * mDensity;
		//		mSections = new String[] { "定位", "热门", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
		//				"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
	}

	public void draw(Canvas canvas) {

		if (mSections != null && mSections.length > 0) {
			// Preview is shown when mCurrentSection is set
			if (mCurrentSection >= 0) {
				String text = mSections[mCurrentSection];
				Paint previewPaint = new Paint();
				previewPaint.setColor(Color.BLACK);
				previewPaint.setAlpha(96);
				previewPaint.setAntiAlias(true);
				previewPaint.setShadowLayer(3, 0, 0, Color.argb(64, 0, 0, 0));

				Paint previewTextPaint = new Paint();
				previewTextPaint.setColor(Color.WHITE);
				previewTextPaint.setAntiAlias(true);

				if (text.length() > 1) {
					previewTextPaint.setTextSize(25 * mScaledDensity);
				} else {
					previewTextPaint.setTextSize(50 * mScaledDensity);
				}
				float previewTextWidth = 2 * mPreviewPadding + previewTextPaint.measureText(text);
				float previewTextHeight = 2 * mPreviewPadding + previewTextPaint.descent() - previewTextPaint.ascent();
				float size = Math.max(previewTextWidth, previewTextHeight);

				RectF previewRect = new RectF((mListViewWidth - size) / 2, (mListViewHeight - size) / 2,
						(mListViewWidth - size) / 2 + size, (mListViewHeight - size) / 2 + size);

				canvas.drawRoundRect(previewRect, 5 * mDensity, 5 * mDensity, previewPaint);
				canvas.drawText(text, previewRect.left + mPreviewPadding + (size - previewTextWidth) / 2,
						previewRect.bottom - previewTextPaint.descent() - mPreviewPadding - (size - previewTextHeight)
								/ 2, previewTextPaint);
				//				canvas.drawText(mSections[mCurrentSection], previewRect.left + (size - previewTextWidth) / 2 - 1, previewRect.top
				//						+ mPreviewPadding - previewTextPaint.ascent() + 1, previewTextPaint);
			}

			Paint indexPaint = new Paint();
			indexPaint.setColor(Color.parseColor("#999999"));
			indexPaint.setAlpha((int) (255 * mAlphaRate));
			indexPaint.setAntiAlias(true);
			indexPaint.setTextSize(12 * mScaledDensity);

			Paint indexPaint2 = new Paint(indexPaint);
			indexPaint2.setTextSize(10 * mScaledDensity);

			float sectionHeight = (mIndexbarRect.height() - 2 * mIndexbarMargin) / mSections.length;
			float paddingTop = (sectionHeight - (indexPaint.descent() - indexPaint.ascent())) / 2;
			for (int i = 0; i < mSections.length; i++) {
				if (i == 0 || i == 1) {
					float paddingLeft = (mIndexbarWidth - indexPaint2.measureText(mSections[i])) / 2;
					canvas.drawText(mSections[i], mIndexbarRect.left + paddingLeft, mIndexbarRect.top + mIndexbarMargin
							+ sectionHeight * i + paddingTop - indexPaint.ascent(), indexPaint2);
				} else {
					float paddingLeft = (mIndexbarWidth - indexPaint.measureText(mSections[i])) / 2;
					canvas.drawText(mSections[i], mIndexbarRect.left + paddingLeft, mIndexbarRect.top + mIndexbarMargin
							+ sectionHeight * i + paddingTop - indexPaint.ascent(), indexPaint);
				}
			}
		}
	}

	public boolean onTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			// If down event occurs inside index bar region, start indexing
			if (contains(ev.getX(), ev.getY())) {

				// It demonstrates that the motion event started from index bar
				mIsIndexing = true;
				// Determine which section the point is in, and move the list to that section
				mCurrentSection = getSectionByPoint(ev.getY());
				mListView.setSelection(mIndexer.getPositionForSection(mCurrentSection));
				return true;
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if (mIsIndexing) {
				// If this event moves inside index bar
				if (contains(ev.getX(), ev.getY())) {
					// Determine which section the point is in, and move the list to that section
					mCurrentSection = getSectionByPoint(ev.getY());
					mListView.setSelection(mIndexer.getPositionForSection(mCurrentSection));
				}
				return true;
			}
			break;
		case MotionEvent.ACTION_UP:
			if (mIsIndexing) {
				mIsIndexing = false;
				mCurrentSection = -1;
			}
			break;
		}
		return false;
	}

	public void onSizeChanged(int w, int h, int oldw, int oldh) {
		mListViewWidth = w;
		mListViewHeight = h;
		mIndexbarRect = new RectF(w - mIndexbarMargin - mIndexbarWidth, mIndexbarMargin, w - mIndexbarMargin, h
				- mIndexbarMargin);
	}

	public void setAdapter(Adapter adapter) {
		if (adapter instanceof SectionIndexer) {
			mIndexer = (SectionIndexer) adapter;
			mSections = (String[]) mIndexer.getSections();
		}
	}

	public boolean contains(float x, float y) {
		// Determine if the point is in index bar region, which includes the right margin of the bar
		return (x >= mIndexbarRect.left && y >= mIndexbarRect.top && y <= mIndexbarRect.top + mIndexbarRect.height());
	}

	private int getSectionByPoint(float y) {
		if (mSections == null || mSections.length == 0)
			return 0;
		if (y < mIndexbarRect.top + mIndexbarMargin)
			return 0;
		if (y >= mIndexbarRect.top + mIndexbarRect.height() - mIndexbarMargin)
			return mSections.length - 1;
		return (int) ((y - mIndexbarRect.top - mIndexbarMargin) / ((mIndexbarRect.height() - 2 * mIndexbarMargin) / mSections.length));
	}

}
